package ModelLayer;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

public class Statistics {

	private CustomerRecords customerRecords;
	private EmployeeRecords employeeRecords;
	private SalesRecords salesRecords;
	
	public Statistics()
	{
		customerRecords=CustomerRecords.getInstance();
		salesRecords=SalesRecords.getInstance();
		employeeRecords=EmployeeRecords.getInstance();
	}
	
	public ArrayList<String> printCustomerStatistics(int id, int month, int year) throws RealException
	{
		SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
        formatter.setLenient(false);
		ArrayList<String> cString=new ArrayList<String>();
		//customerRecords.getCustomer(id);
		cString.add("\n");
		for(Sale sale: salesRecords.getSalesRecords())
		{
			if(sale.getCustomer().getId() == customerRecords.getCustomer(id).getId() && sale.getMonth()==month && sale.getYear()==year)
			{
				cString.add("ID: "+sale.getId()+", Price: "+Double.valueOf(new DecimalFormat("#.##").format(sale.getFinalPrice()))+", Date: "+ formatter.format(sale.getStartDay()));
			}
		}
		return cString;
	}
	
	public double getTotalPriceCustomer(int id, int month, int year) throws RealException
	{
		double total=0;
		for(Sale sale: salesRecords.getSalesRecords())
		{
			if(sale.getCustomer().getId() == customerRecords.getCustomer(id).getId() && sale.getMonth()==month && sale.getYear()==year)
			{
				total += sale.getFinalPrice();
			}
		}
		return total;
	}
	
	public ArrayList<String> printSalesmanStatistics(int id, int month, int year) throws RealException
	{
		SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
        formatter.setLenient(false);
		ArrayList<String> cString=new ArrayList<String>();
		//customerRecords.getCustomer(id);
		cString.add("\n");
		for(Sale sale: salesRecords.getSalesRecords())
		{
			if(sale.getEmployee().getId() == employeeRecords.getEmployee(id).getId() && sale.getMonth()==month && sale.getYear()==year)
			{
				cString.add("ID: "+sale.getId()+", Price: "+Double.valueOf(new DecimalFormat("#.##").format(sale.getFinalPrice()))+", Date: "+ formatter.format(sale.getStartDay()));
			}
		}
		return cString;
	}
	
	public double getTotalPriceSaleman(int id, int month, int year) throws RealException
	{
		double total=0;
		for(Sale sale: salesRecords.getSalesRecords())
		{
			if(sale.getEmployee().getId() == employeeRecords.getEmployee(id).getId() && sale.getMonth()==month && sale.getYear()==year)
			{
				total += sale.getFinalPrice();
			}
		}
		return total;
	}
}
